#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Name : __init__.py
# Author: ls_seraph@126.com
# Time  : 2021/3/6 22:46

import os
import yaml
import sys
from frame.common.global_data.path_param import Param

# 配置文件字典
_yaml_dict = {}


class ConfigYamlUtil:
    def __init__(self):
        # 字符串转成字典
        stream = open(os.path.join(Param.get_value('path'), 'frame/config/config.yml'), 'r', encoding='utf-8')
        data = stream.read()
        stream.close()
        global _yaml_dict
        _yaml_dict = yaml.load(data, Loader=yaml.FullLoader)

    def get_dict(self):
        return _yaml_dict

    def get_option(self, name):
        """
        获取配置项\n
        :param name: 配置项名称
        :return: name配置项集合
        """
        try:
            ob = _yaml_dict.get(name)
        except KeyError:
            print("config.yml没有找到对应关键字[{0}}]".format(name), file=sys.stderr)
            ob = {}
        return ob

    def get_default_value(self, key):
        """
        获取默认配置\n
        :param key: 默认配置关键字
        :return: 关键字对应的值
        """
        try:
            val = _yaml_dict["default"][key]
        except KeyError:
            print("config.yml没有找到对应关键字[default][{0}]".format(key), file=sys.stderr)
            val = ""
        return val

    def get_log_value(self, key):
        """
        获取日志配置\n
        :param key: 默认配置关键字
        :return: 关键字对应的值
        """
        try:
            val = _yaml_dict["log"][key]
        except KeyError:
            print("config.yml没有找到对应关键字[log][{0}]".format(key), file=sys.stderr)
            val = ""
        return val

    def get_driver_value(self, key):
        """
        获取驱动配置\n
        :param key: 默认配置关键字
        :return: 关键字对应的值
        """
        try:
            val = _yaml_dict["driver"][key]
        except KeyError:
            print("config.yml没有找到对应关键字[driver][{0}]".format(key), file=sys.stderr)
            val = ""
        return val

    def get_path_value(self, key):
        """
        获取路径配置\n
        :param key: 邮件配置关键字
        :return: 关键字对应的值
        """
        try:
            val = _yaml_dict["path"][key]
        except KeyError:
            print("config.yml没有找到对应关键字[path][{0}]".format(key), file=sys.stderr)
            val = ""
        return val

    def get_report_value(self, key):
        """
        获取报告配置\n
        :param key: 报告配置关键字
        :return: 关键字对应的值
        """
        try:
            val = _yaml_dict["report"][key]
        except KeyError:
            print("config.yml没有找到对应关键字[report][{0}]".format(key), file=sys.stderr)
            val = ""
        return val

    def get_email_value(self, key):
        """
        获取邮件配置\n
        :param key: 邮件配置关键字
        :return: 关键字对应的值
        """
        try:
            val = _yaml_dict["email"][key]
        except KeyError:
            print("config.yml没有找到对应关键字[email][{0}]".format(key), file=sys.stderr)
            val = ""
        return val


config = ConfigYamlUtil()
